package com.example.testthread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {
	static TextView tv;
	static Handler handler = new Handler() {
		//1.handler的looper是用默认的mLooper=Looper.myLooper();
		//2.mQueue 通过mLooper直接获得
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 0) {
				tv.setText("aaaaaaaaaaaa" + msg.arg1);
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		
		new Thread(){

			@Override
			public void run() {
				for(int i=0; i<10; i++) {
					Message m = Message.obtain();
					m.what = 0;
					m.arg1 = i;
					//3.设置Msg的messageQueue为当前handler的msgQ，
					//4.把Msg的target设置为handler自己
					//5.把msg放到handler的msgQ里
					handler.sendMessage(m);
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}.start();
	}
}
